'use strict';

const Controller = require('egg').Controller;
class TopicController extends Controller {
  async getTopicCateList() {
    const { ctx } = this;
    const { list } = await ctx.service.topic.getTopicCateList();
    return ctx.helper.success({
      ctx,
      data: list,
    });
  }

  async getTopicList() {
    const { ctx } = this;
    const topicTabId = ctx.params.id;
    const { list } = await ctx.service.topic.getTopicList(topicTabId);
    return ctx.helper.success({
      ctx,
      data: list,
    });
  }

  async getTopicDetail() {
    const { ctx } = this;
    const topicId = ctx.params.id;
    const { data } = await ctx.service.topic.getTopicDetail(topicId);
    return ctx.helper.success({
      ctx,
      data: data[0],
    });
  }

  async addTopic() {
    const { ctx } = this;
    const topicRule = {
      user_id: 'number',
      publish_time: 'number',
      content: 'string',
      tab_id: 'number',
    };
    const data = ctx.request.body;
    data.user_id = ctx.state.user.id;
    data.publish_time = new Date().getTime();
    ctx.validate(topicRule, data);
    const { id } = await ctx.service.topic.addTopic(data);
    return ctx.helper.success({
      ctx,
      data: { id },
    });
  }

  async getTopicCommentList() {
    const { ctx } = this;
    const topicId = ctx.params.id;
    const userId = ctx.state.user.id;
    const { list } = await ctx.service.topic.getTopicCommentList(
      topicId,
      userId
    );
    return ctx.helper.success({
      ctx,
      data: list,
    });
  }

  async addTopicComment() {
    const { ctx } = this;
    const topicCommentRule = {
      topic_id: 'number',
      content: 'string',
    };
    const data = ctx.request.body;
    data.user_id = ctx.state.user.id;
    data.publish_time = new Date().getTime();
    ctx.validate(topicCommentRule, data);
    const { id } = await ctx.service.topic.addTopicComment(data);
    return ctx.helper.success({
      ctx,
      data: { id },
    });
  }

  async deleteTopicComment() {
    const { ctx } = this;
    const commentId = ctx.params.id;
    const userId = ctx.state.user.id;
    const { deleteSuccess } = await ctx.service.topic.deleteTopicComment(
      Number(commentId),
      userId
    );
    return ctx.helper.success({
      ctx,
      msg: deleteSuccess ? '删除评论成功' : '删除评论失败',
    });
  }

  async likeTopicComment() {
    const { ctx } = this;
    const commentId = ctx.params.id;
    const userId = ctx.state.user.id;
    const { insertSuccess } = await ctx.service.topic.likeTopicComment(
      Number(commentId),
      Number(userId)
    );
    return ctx.helper.success({
      ctx,
      msg: insertSuccess ? '点赞成功' : '点赞失败',
    });
  }

  async dislikeTopicComment() {
    const { ctx } = this;
    const commentId = ctx.params.id;
    const userId = ctx.state.user.id;
    const { deleteSuccess } = await ctx.service.topic.dislikeTopicComment(
      Number(commentId),
      Number(userId)
    );
    return ctx.helper.success({
      ctx,
      msg: deleteSuccess ? '取消点赞成功' : '取消点赞失败',
    });
  }
}

module.exports = TopicController;
